﻿using Face8280WebSocketServer.DB.Device;
using Face8280WebSocketServer.Interface;
using Face8280WebSocketServer.Utility.FilterAttribute;
using Face8280WebSocketServer.Utility.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Face8280WebSocketServer.Controllers.FaceDevice.Remote
{
    [Route("api/[controller]")]
    [ApiController]
    public class DeviceRemoteController : Controller
    {

        private readonly ILogger<DeviceRemoteController> _logger;

        private IDeviceRemoteService _RemoteDB;

        public DeviceRemoteController(ILogger<DeviceRemoteController> logger,
            IDeviceRemoteService door)
        {
            _logger = logger;
            _RemoteDB = door;


        }

        [HttpPost]
        [Route("Add")]
        [Authorize]
        [TypeFilter(typeof(VerifyActionFilterAttribute))]
        public IActionResult Add([FromForm] DeviceRemoteAddParameter par)
        {

            if (par.DeviceIDs == null)
            {
                return new JsonResult(new JsonResultModel(2, "未选择需要操作的设备"));
            }
            if (par.DeviceIDs.Count == 0)
            {
                return new JsonResult(new JsonResultModel(2, "未选择需要操作的设备"));
            }

            _RemoteDB.Add(par.DeviceIDs,par.Remote);
            return new JsonResult(new JsonResultModel());
        }


        [HttpPost]
        [Route("Delete")]
        [Authorize]
        [TypeFilter(typeof(VerifyActionFilterAttribute))]
        public IActionResult Delete([FromForm] DeviceRemoteDeleteParameter par)
        {

            if (par.TaskIDs == null)
            {
                return new JsonResult(new JsonResultModel(2, "未选择需要操作的远程任务"));
            }
            if (par.TaskIDs.Count == 0)
            {
                return new JsonResult(new JsonResultModel(2, "未选择需要操作的远程任务"));
            }

            _RemoteDB.Delete(par.TaskIDs);
            return new JsonResult(new JsonResultModel());
        }



        /// <summary>
        /// 获取远程操作任务列表
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetList")]
        [Authorize]
        [TypeFilter(typeof(VerifyActionFilterAttribute))]
        public IActionResult GetList([FromForm] DeviceRemoteQueryParameter par)
        {
            //_logger.LogInformation("获取远程任务列表");
            var devices = _RemoteDB.GetList(par.DeviceID, par.DeviceName, par.Remote, par);

            return new JsonResult(new JsonResultModel(devices));
        }

        
        /// <summary>
        /// 清空出入记录
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("ClearRemote")]
        [Authorize]
        public IActionResult ClearRemote()
        {
            //_logger.LogInformation("清空远程操作记录");
            _RemoteDB.ClearRemote();

            return new JsonResult(new JsonResultModel());
        }
    }
}
